isListen = false
isStart = false
srv = nil
conn = nil

mulIp = "224.0.211.211"
--mulIp = "192.168.50.118"
mulListenPort = 6100
mulGatewayPort = 6000
--wifi.eventmon.register(wifi.eventmon.STA_CONNECTED, function(T)
--    print("STA_CONNECTED  SSID="..T.SSID)
-- end)
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
    print("STA_DISCONNECTED")
    if(isListen) then
        srv:close()
        conn:close()
        conn = nil
        srv = nil
        isListen = false
        print("srv.close()")
    end
 end)

wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
    print("get_ip")
    if(srv) then
        print("if(srv) = true")
        srv:close()
    end
    if(conn) then
        print("if(conn) = true")
        conn:close()
    end
    net.multicastJoin("", mulIp)
    srv = net.createServer(net.UDP)
    conn = net.createConnection(net.UDP, 0)
    conn:connect(mulGatewayPort,mulIp)
    srv:on("receive", 
      function(s, data, port, ip) -- port ip 都为nil
        print("received:"..data)
        conn:send("111111111110000000")
        s:send("99999")
        srv:send("8888")
      end
    )
    srv:listen(mulListenPort)
    
    isListen = true
    isStart = true
 end)

wifi.setmode(wifi.STATION)
wifi.sta.config('A701', 'a15880287827')
